package org.gp.Interfaz;

import javax.swing.JDialog;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.JPasswordField;

import org.gp.tringes.GestorIGInicio;
import org.gp.tringes.GestorIGRegistroClub;


public class IGRegistroClub extends JDialog {
	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblNombre;
	private JTextField txtNombre;
	private JLabel lblPatrocinador;
	private JTextField txtPatrocinador;
	private JLabel lblContrasea;
	private JPasswordField txtContrasea;
	private JLabel lblRepetirContrasea;
	private JPasswordField txtRepetirContrasea;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JLabel lblInvisible;
	private JLabel lblLimite;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGRegistroClub dialog = new IGRegistroClub();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGRegistroClub() {
		setBounds(100, 100, 415, 260);
		//ID-192
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(192));
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		getContentPane().add(getPanel_2(), BorderLayout.CENTER);
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
		}
		return panel;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
			panel_1.add(getBtnAceptar());
			panel_1.add(getBtnCancelar());
		}
		return panel_1;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.setLayout(new GridLayout(5, 2, 0, 10));
			panel_2.add(getLblNombre());
			panel_2.add(getTxtNombre());
			panel_2.add(getLblPatrocinador());
			panel_2.add(getTxtPatrocinador());
			panel_2.add(getLblContrasea());
			panel_2.add(getTxtContrasea());
			panel_2.add(getLblInvisible());
			panel_2.add(getLblLimite());
			panel_2.add(getLblRepetirContrasea());
			panel_2.add(getTxtRepetirContrasea());
		}
		return panel_2;
	}
	
	private JLabel getLblNombre(){
		if(lblNombre == null){
			//ID-110
			lblNombre = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(110));
			lblNombre.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNombre;
	}
	private JTextField getTxtNombre(){
		if(txtNombre == null){
			txtNombre = new JTextField();
			txtNombre.setColumns(10);
		}
		return txtNombre;
	}
	
	private JLabel getLblPatrocinador(){
		if(lblPatrocinador == null){
			//ID-112
			lblPatrocinador = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(112));
			lblPatrocinador.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblPatrocinador;
	}
	private JTextField getTxtPatrocinador(){
		if(txtPatrocinador == null){
			txtPatrocinador = new JTextField();
			txtPatrocinador.setColumns(10);
		}
		return txtPatrocinador;
	}
	
	private JLabel getLblContrasea(){
		if(lblContrasea == null){
			//ID-114
			lblContrasea = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(114));
			lblContrasea.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblContrasea;
	}
	private JPasswordField getTxtContrasea(){
		if(txtContrasea == null){
			txtContrasea = new JPasswordField();
			txtContrasea.setColumns(10);
		}
		return txtContrasea;
	}
	
	private JLabel getLblRepetirContrasea(){
		if(lblRepetirContrasea == null){
			//ID-193
			lblRepetirContrasea = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(193));
			lblRepetirContrasea.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblRepetirContrasea;
	}
	private JPasswordField getTxtRepetirContrasea(){
		if(txtRepetirContrasea == null){
			txtRepetirContrasea = new JPasswordField();
			txtRepetirContrasea.setColumns(10);
		}
		return txtRepetirContrasea;
	}
	
	private JButton getBtnAceptar(){
		if(btnAceptar == null){
			//ID-2
			btnAceptar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(2));
			btnAceptar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					try{
						Boolean longitudNombre = GestorIGRegistroClub.getGestorIGRegistroClub().comprobarLongitudNombre(txtNombre.getText());
						if(longitudNombre){
							Boolean compContra = GestorIGRegistroClub.getGestorIGRegistroClub().comprobarContrasea(txtContrasea.getPassword(), txtRepetirContrasea.getPassword());
							if(compContra){
								Boolean existeClub = GestorIGRegistroClub.getGestorIGRegistroClub().existeClub(txtNombre.getText());
								if(!existeClub){
									GestorIGRegistroClub.getGestorIGRegistroClub().insertarClub(txtNombre.getText(), txtPatrocinador.getText(), txtContrasea.getPassword());
									//ID-21
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(21));
									dispose();
								}else{
									//ID-185
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(185));
								}
							}else{
								//ID-181
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(181));
							}
						}else{
							//ID-194
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(194));
						}
					} catch (SQLException e) {
					e.printStackTrace();
					}
				}
			});
		}
		return btnAceptar;
	}
	
	
	private JButton getBtnCancelar(){
		if(btnCancelar == null){
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}

	private JLabel getLblInvisible() {
		if (lblInvisible == null) {
			lblInvisible = new JLabel("");
		}
		return lblInvisible;
	}
	private JLabel getLblLimite() {
		if (lblLimite == null) {
			//ID-179
			lblLimite = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(179));
		}
		return lblLimite;
	}
}